﻿package components {
	
	import com.limetalks.data.Stream;
	import com.limetalks.events.StreamSelected;
	
	import flash.display.MovieClip;

	public class SphereClient extends MovieClip {
		
		private var sphere:Sphere;
		
		private var streamArray:Array;
		
		public function SphereClient(array:Array):void {

			streamArray = array;
			// add sphere
			sphere = new Sphere(streamArray,this,170);
			addChild(sphere);
			sphere.x = 220;//this.stage.stageWidth/3;
			sphere.y = 250;//this.stage.stageHeight/3;  
		 }
		
		public function streamSelected(stream:Stream):void {
			
			dispatchEvent(new StreamSelected(stream));
		}
		
		public function addStream(stream:Stream):void {
			
			streamArray.push(stream);
			updateStreams();
		}
		
		public function removeStream(stream:Stream):void {
			
			var indexToRemove:int = 0;
			streamArray.forEach( function(element:*, index:int, arr:Array) {
				if((Stream(streamArray[index])).id == stream.id)
					indexToRemove = index;
			});
			streamArray.splice(indexToRemove,1);
			updateStreams();
		}
		
		public function updateSphere(streams:Array):void {
			streamArray = streams;
			updateStreams();
		}
		
		private function updateStreams():void {
			sphere.update(streamArray);
		}
		
		public function setCategory(categoryId:int):void {
			
		}
	}
}
